package com.jocund.quicktest;

import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.ReentrantLock;

public class MessageDispatcher implements Runnable {
	private BlockingQueue<Message> messages;
	private HashMap<Integer, Conversation> conversationMap;
	
	public MessageDispatcher(BlockingQueue<Message> messages,
			HashMap<Integer, Conversation> conversationMap){
		this.messages = messages;
		this.conversationMap = conversationMap;
		//ArrayBlockingQueue<Message> mmm = new ArrayBlockingQueue<Message>(100);
	}
	
	public void run() {
		while (true){
			try {
				Message msg = messages.take();
				int conversationId = msg.getConversationId();
				Conversation conv = conversationMap.get(conversationId);
				for(User user : conv.getGroup().getUserMap().values()){
					conv.pushMessage(user.getUniqueName(), msg);
				}
				//conv.pushMessage(userUniqueName, msg);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UserExpiredException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
